package cate.game.server.controller;

import cate.game.GameBody;
import cate.game.ManyGame;
import cate.game.activity.RoleActivity;
import cate.game.db.mongo.RoleActivityDao;
import cate.game.framework.ThreadShare;
import cate.game.role.Role;
import cate.game.stat.GameCT;
import easy.java.spring.annotation.MyGetMapping;
import easy.java.util.NumTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;
import java.util.concurrent.CompletableFuture;

@Slf4j
@RequestMapping("")
@Controller
public class RootController extends GameSrvBaseController {

    private ManyGame games;
    @Autowired
    public void setGames(ManyGame games) {
        this.games = games;
    }

    @MyGetMapping("ct")
    @ResponseBody
    public List<GameCT> ct() {
        List<GameCT> list = new ArrayList<>();
        for(GameBody g : games.getAll()) {
            list.add(g.stat.ct());
        }
        return list;
    }

    @MyGetMapping("xxx")
    @ResponseBody
    public Object xxx() throws Exception {
        Map<String, Object> map = new HashMap<>();
        GameBody body = games.getAll().get(0);
        RoleActivityDao dao = body.facilities.getMongo().activity;

        RoleActivity sample = dao.findById("642696f7b62b797d00989b66").get();


        Set<Integer> saveHashSets = new HashSet<>();
        List<String> saveList = new ArrayList<>();
        Set<Integer> findHashSets = new HashSet<>();
        List<Integer> findHashList = new ArrayList<>();
        List<Thread> tl = new ArrayList<>();
        for(int i = 0; i < 30; i++) {
            tl.add(new Thread(() -> {
                RoleActivity saved = dao.save(sample);
                saveList.add(saved.uid);
                saveHashSets.add(saved.hashCode());
            }));
            tl.add(new Thread(() -> {
                Optional<RoleActivity> act = dao.findById("642696f7b62b797d00989b66");
                if(act != null && act.isPresent()) {
                    findHashSets.add(act.get().hashCode());
                    findHashList.add(act.get().hashCode());
                } else {
                    log.info("居然为null");
                }
            }));
        }
        for(Thread t : tl) {
            t.start();
        }
        for(Thread t : tl) {
            t.join();
        }
        map.put("saveList", saveList);
        map.put("saveHashSets", saveHashSets);
        map.put("findHashSets", findHashSets);
        map.put("findHashList", findHashList);
        return map;
    }

    @MyGetMapping("yyy")
    @ResponseBody
    public Object yyy() throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("yyy", "yyy");
        GameBody body = games.getAll().get(0);
        RoleActivityDao dao = body.facilities.getMongo().activity;

        RoleActivity sample = dao.findById("642696f7b62b797d00989b66").get();

        final int batch = 40;

        List<CompletableFuture<String>> strFutureList = new ArrayList<>();
        for(int i = 0; i < batch; i++) {
            final int no = i;
            CompletableFuture<String> strFuture = CompletableFuture.supplyAsync(() -> {
                String ret = null;
                try {
                    log.info("第[{}]号任务开始执行", no);
                    if(no % 2 == 0) {
                        RoleActivity saved = dao.save(sample);
                        ret = "（保存）" + saved.hashCode();
                    } else {
                        Optional<RoleActivity> act = dao.findById("642696f7b62b797d00989b66");
                        ret = "（读取）" + act.isPresent();
                    }
                } catch (Exception e) {
                    log.error("", e);
                }
                log.info("第[{}]号任务 已完成", no);
                return "结果"+no+":"+ret;
            });
            strFutureList.add(strFuture);
        }

        for(int i = 0; i < batch; i++) {
            final int tIndex = i;
            final int no = i;
            ThreadShare.instance().exe(tIndex, () -> {
                try {
                    log.info("**第[{}]号任务开始执行", no);
                    if(no % 2 == 0) {
                        RoleActivity saved = dao.save(sample);
                    } else {
                        Optional<RoleActivity> act = dao.findById("642696f7b62b797d00989b66");
                    }
                    log.info("**第[{}]号任务 已完成", no);
                } catch (Exception e) {
                    log.error("", e);
                }
            });
        }

        new Thread(() -> {
            for(CompletableFuture<String> future : strFutureList) {
                try {
                    String str = future.get();
                    log.info("收货:{}", str);
                } catch (Exception e) {
                    log.error("", e);
                }
            }
        }).start();
        return map;
    }


    @MyGetMapping("dui")
    @ResponseBody
    public Object dui() throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("dui", "dui");
        GameBody body = games.getAll().get(0);
        RoleActivityDao dao = body.facilities.getMongo().activity;
        Optional<RoleActivity> act = dao.findById("642696f7b62b797d00989b66");
        map.put("act", act.isPresent());
        return map;
    }

    @MyGetMapping("cuo")
    @ResponseBody
    public Object cuo() throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("cuo", "cuo");
        GameBody body = games.getAll().get(0);
        RoleActivityDao dao = body.facilities.getMongo().activity;
        Optional<RoleActivity> act = dao.findById("645318ffe8b4547fab51051f");
        map.put("act", act.get());
        return map;
    }


}
